package com.itheima.store.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.itheima.store.dao.BookCategoryDao;
import com.itheima.store.domain.BookCategory;
import com.itheima.store.utils.JDBCUtils;
/**
 * 分类的DAO的实现类
 * @author admin
 *
 */
public class BookCategoryDaoImpl implements BookCategoryDao {

	@Override
	public List<BookCategory> findAll() throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from books_category ORDER BY createTime DESC ";
		List<BookCategory> list = queryRunner.query(sql, new BeanListHandler<BookCategory>(BookCategory.class));
		return list;
	}

	@Override
	public void save(BookCategory category) throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql ="insert into books_category values(?,?,?,?,?)";
		int i = queryRunner.update(sql, category.getCategory_id(),category.getCategory_name(),category.getCreateTime(),
				category.getCreatePerson_id(),category.getCreatePerson());
		
	}

	@Override
	public BookCategory findByCid(String cid) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from books_category where category_id=?";
		BookCategory category = queryRunner.query(sql, new BeanHandler<BookCategory>(BookCategory.class), cid);
		return category;
	}

	@Override
	public void update(BookCategory category) throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "update books_category set category_name =? where category_id=?";
		queryRunner.update(sql, category.getCategory_name(),category.getCategory_id());
	}

	@Override
	public void deleteCategoryByCid(String cid) throws SQLException {
		// TODO Auto-generated method stub
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "delete from books_category where category_id =?";
		queryRunner.update(sql, cid);
		
	}

	@Override
	public Integer findCount() throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select count(*) from books_category ";
		Long count = (Long) queryRunner.query(sql, new ScalarHandler());
		return count.intValue();
	}

	@Override
	public List<BookCategory> findPage(int begin, Integer pageSize) throws SQLException {
		QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource());
		String sql = "select * from books_category ORDER BY createTime DESC limit ?,?";
		List<BookCategory> list = queryRunner.query(sql, new BeanListHandler<BookCategory>(BookCategory.class),begin,pageSize);
		return list;
	}

}
